package com.lk.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lk.springboot.service.HomeSevice;
import com.lk.springboot.service.LoginService;
import com.lk.springboot.vo.Result;
import com.lk.springboot.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lk
 * @date 2020/3/5 0005 12:04
 * @description: 同时模块
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    LoginService loginService;
    @Autowired
    HomeSevice homeSevice;

    /**
     * 通知列表（目前仅为贡献列表）
     *
     * @param token
     * @param start
     * @param size
     * @return
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public String listNotice(@CookieValue("token") String token,
                             @RequestParam(value = "start", defaultValue = "1") int start,
                             @RequestParam(value = "size", defaultValue = "15") int size) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            homeSevice.setNewsStatus(u_id);// 每次一打开通知列表，就将状态设为已读
            Page page = PageHelper.startPage(start, size, "date desc");
            List<JSONObject> list = homeSevice.findAllMyAchievements(u_id);
            PageInfo<JSONObject> pageInfo = new PageInfo<>(list);
            pageInfo.setPages(page.getPages());//总页数
            pageInfo.setTotal(page.getTotal());//总条数
            pageInfo.setPageNum(page.getPageNum());//当前页
            Result result = new Result(ResultCode.SUCCESS);
            result.setData(pageInfo);
            return JSONObject.toJSONString(result);
        }
    }

    /**
     * 获取通知列表状态（是否有新消息）
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/status", method = RequestMethod.GET)
    public String getNoticeStatus(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = homeSevice.getNewsStatus(u_id);
            return result;
        }

    }
}
